package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

public class SnakeJFrame extends JFrame {
    private  snake snake;
    private Timer timer;//定时器
    private  JPanel jPanel;
    private Node food;


    public SnakeJFrame() throws HeadlessException {
        //初始化窗体的参数
        initjframe();
        //初始化棋盘
        initGamePanel();
        //初始化蛇
        ininSnake();
        //初始化定时器
        initTimer();
        //设置键盘监听，让蛇朝着上下左右移动
        setKeyListen();
        //食物
        initfood();


    }

    private void initfood() {
        food = new Node();
        food.ramdom();
    }

    private void setKeyListen() {
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //System.out.println("键盘按下");
                //键盘每一个键都有一个编号
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                        if (snake.getDirection()!=Direction.down){
                            snake.setDirection(Direction.up);
                        }
                        break;
                   case KeyEvent.VK_DOWN:
                    if (snake.getDirection()!=Direction.up){
                        snake.setDirection(Direction.down);
                    }
                         break;
                   case KeyEvent.VK_LEFT:
                       if (snake.getDirection()!=Direction.right){
                           snake.setDirection(Direction.left);
                       }
                       break;
                   case KeyEvent.VK_RIGHT:
                       if (snake.getDirection()!=Direction.left){
                           snake.setDirection(Direction.right);
                       }
                       break;
                }
            }
        });
    }

    //初始化定时器
    private void initTimer() {
        timer = new Timer();
        //初始化定时任务
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                snake.move();
                //判断蛇是否吃了食物
                Node head = snake.getBody().getFirst();
                if (head.getX()== food.getX()&&head.getY()== food.getY()){
                    snake.eat(food);
                    food.ramdom();
                }
                //重新绘制游戏棋盘
                jPanel.repaint();
            }
        };
        timer.scheduleAtFixedRate(task, 100, 100);
    }

    private void ininSnake(){
        snake = new snake();
    }
    private void initGamePanel(){
         jPanel = new JPanel(){
            //绘制棋盘的内容
            @Override
            public void paint(Graphics g) {
                //清空棋盘
                g.clearRect(0, 0, 600, 600);
                //绘制40条横线
                for (int i = 0; i <= 40; i++) {
                    g.drawLine(0,15*i,600,i*15);
                }
                //绘制40条竖线
                for (int i = 0; i <= 40; i++) {
                    g.drawLine(15*i,0,15*i,600);
                }
                //绘制蛇
                LinkedList<Node> body = snake.getBody();
                for(Node node : body){
                    g.fillRect(node.getX()*15,node.getY()*15,15,15);
                }
                //绘制食物
                g.fillRect(food.getX()*15,food.getY()*15,15,15);

            }
        };
        add(jPanel);

    }
    private void initjframe() {
        //设置窗体的宽高
        setSize(618,648);
        //设置窗体的位置
        setLocation(400,400);
        //设置关闭按钮的作用
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置大小不可变
        setResizable(false);

    }

    public static void main(String[] args) {
        //创建窗体对象
        new  SnakeJFrame().setVisible(true);
    }
}
